﻿using System;
using System.Net;
using System.Security;
using System.Web.Security;
using Common;


namespace SprintChart
{
    public partial class LogOn : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void Logon_Click(object sender, EventArgs e)
        {
            NetworkCredential networkCredential = GetNetworkCredentials(TfsUserId.Text, UserPass.Text);
            if (TfsAuthenticator.AuthenticateTfsUser(networkCredential))
            {
                Session["IsUserAuthenticated"] = true;
                Session["NetworkCredential"] = networkCredential;
                FormsAuthentication.RedirectFromLoginPage
                   (TfsUserId.Text, Persist.Checked);
            }
            else
            {
                Msg.Text = "Invalid credentials. Please try again.";
            }
        }
        private NetworkCredential GetNetworkCredentials(string userName, String password)
        {
            SecureString securePassword = new SecureString();
            foreach(char currentChar in password.ToCharArray())
            {
                securePassword.AppendChar(currentChar);
            }
            return  new NetworkCredential(userName,securePassword);
        }
    }
}